package com.gla.giliair.web.customer;

import com.gla.giliair.entity.MOrder;
import com.gla.giliair.web.BaseController;
import com.gla.giliair.web.vo.CustomerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @Description:
 * @Author: Yingtian qian
 * @Date: 2021/8/25 16:20
 */
@Api(tags = "支付模块")
@Controller
public class PayController extends BaseController {

    @ApiOperation(value = "免费支付", notes = "免费支付，修改数据库状态", produces = "application/json")
    @PostMapping("/api/user/freePay")
    @ResponseBody
    public Map<String, Object> freePay(@RequestParam("orderId") Long orderId,
                                       HttpSession httpSession) {
        CustomerVO userTemp = (CustomerVO) httpSession.getAttribute("user");

        MOrder mOrder = new MOrder();
        mOrder.setCustomerId(userTemp.getCustomerId());
        mOrder.setOrderId(orderId);

        Map<String, Object> res = orderService.freePay(mOrder);
        if ((Integer) res.get("status") == 200) {
            delayService.cancelCancelOrderAfter(orderId);
        }
        return res;
    }

    @ApiOperation(value = "直接退票", notes = "直接退票，不经过支付宝，修改数据库状态", produces = "application/json")
    @PostMapping("/api/user/freeRefund")
    @ResponseBody
    public Map<String, Object> freeRefund(@RequestParam("orderId") Long orderId,
                                          HttpSession httpSession) {
        CustomerVO userTemp = (CustomerVO) httpSession.getAttribute("user");

        MOrder mOrder = new MOrder();
        mOrder.setCustomerId(userTemp.getCustomerId());
        mOrder.setOrderId(orderId);

        return orderService.freeRefund(mOrder);
    }

    //临时修改成get
    @ApiOperation(value = "支付宝支付", notes = "支付宝支付，返回二维码页面内容", produces = "text/html;charset=utf-8")
    @GetMapping("/api/user/aliPay")
    @ResponseBody
    public String aliPay(@RequestParam("orderId") Long orderId,
                         HttpSession httpSession) {
        CustomerVO userTemp = (CustomerVO) httpSession.getAttribute("user");

        MOrder mOrder = new MOrder();
        mOrder.setCustomerId(userTemp.getCustomerId());
        mOrder.setOrderId(orderId);

        return alipayService.aliPay(mOrder);
    }

    @ApiOperation(value = "支付返回", notes = "支付返回，返回成功后修改数据库状态", produces = "text/html;charset=utf-8")
    @GetMapping("/returnUrl")
    public String returnUrl(String out_trade_no, String trade_no, String total_amount, String timestamp) {
        System.out.println("订单号:" + out_trade_no);
        System.out.println("交易时间:" + timestamp);
        System.out.println("交易编号:" + trade_no);
        System.out.println("交易金额:" + total_amount);

        if (StringUtils.hasText(out_trade_no)) {
            MOrder mOrder = new MOrder();
            mOrder.setOrderNumber(out_trade_no);

            Map<String, Object> res = orderService.returnPay(mOrder);
            Integer status = (Integer) res.get("status");
            MOrder mOrderFromDB = (MOrder) res.get("data");

            if (status.equals(200)) {
                delayService.cancelCancelOrderAfter(mOrderFromDB.getOrderId());
                //跳转到成功页面
                return "success";
            } else {
                //内部错误
                return "500";
            }
        }
        //非法访问返回错误页面
        return "error";
    }

    @ApiOperation(value = "支付宝退票", notes = "支付宝退票，退票后修改数据库状态", produces = "application/json")
    @PostMapping("/api/user/aliRefund")
    @ResponseBody
    public Map<String, Object> refundPay(@RequestParam("orderId") Long orderId,
                                         HttpSession httpSession) {
        CustomerVO userTemp = (CustomerVO) httpSession.getAttribute("user");

        MOrder mOrder = new MOrder();
        mOrder.setCustomerId(userTemp.getCustomerId());
        mOrder.setOrderId(orderId);

        return alipayService.aliRefund(mOrder);
    }

    @ApiOperation(value = "申请退票", notes = "申请退票，申请退票后修改数据库状态", produces = "application/json")
    @PostMapping("/api/user/applyRefund")
    @ResponseBody
    public Map<String, Object> applyRefundPay(@RequestParam("orderId") Long orderId,
                                              HttpSession httpSession) {
        CustomerVO userTemp = (CustomerVO) httpSession.getAttribute("user");

        MOrder mOrder = new MOrder();
        mOrder.setCustomerId(userTemp.getCustomerId());
        mOrder.setOrderId(orderId);

        return orderService.applyRefund(mOrder);
    }
}
